
using System;
using CoreMobile.cfg;
using VABMobile.cfg;

namespace VABMobile.print
{
    public class EPrinter : IPrinter
    {
        public static int DEFAULT_PRINTER_PORT = 8;

        private string comPort; //COM9:
        private static int DEFAULT_BAUDRATE = 38400;

        private string printerMsg = null;

        public EPrinter()
        {
            this.comPort = "COM"+SettingsManager<VABSettings>.get().Settings.PrinterPort+":";
        }

        public string PrinterMsg
        {
            get { return printerMsg; }
        }

        private void printBarCode(string barCode)
        {
            if(!string.IsNullOrEmpty(barCode))
            {
                LKPrint.PrintBarCode(barCode
                , LKPrint.LK_BCS_Code128
                , 80 //height in dot units
                , 384 //width in dot units
                , LKPrint.LK_ALIGNMENT_CENTER
                , LKPrint.LK_HRI_TEXT_BELOW);
            }
        }

        public bool print(PrintableDocument document)
        {
            
            
            if(! this.isPrinterReady()) return false;

            //if (!string.IsNullOrEmpty(document.initialBarCode())) 
                this.printBarCode(document.initialBarCode());

             LKPrint.PrintNormal("\x1b|bC\x1b|VAB:: Empresa - "
                 + SettingsManager<VABSettings>.get().Settings.EnterpriseName
                 +"\n");

            foreach (string element in document.bodyText())
            {
               LKPrint.PrintNormal(element+"\n");
            }

            //if (!string.IsNullOrEmpty(document.finalBarCode())) 
                this.printBarCode(document.finalBarCode());

            LKPrint.PrintNormal("\x1b|fP");
            
            return true;
        }

        private bool isPrinterReady()
        {
            
            var lResult = LKPrint.PrinterCheck();

            if (lResult == -1)
            {
                this.printerMsg = "Error Lectura Bluetooth";
                return false;
            }
            if (lResult == -2){

                this.printerMsg = "Error lectura estado Impresora";
                return false;
            }

            lResult = LKPrint.status();

            if (lResult == LKPrint.LK_STS_BATTERY_LOW)
            {
                this.printerMsg = "Bateria baja";
                return false;
            }

            if (lResult == LKPrint.LK_STS_PAPER_EMPTY)
            {
                this.printerMsg = "Papelera vacia";
                return false;
            }

            if (lResult == LKPrint.LK_STS_COVER_OPEN)
            {
                this.printerMsg = "Tapa abierta";
                return false;
            }

            if (lResult == LKPrint.LK_STS_MSR_READ)
            {
                this.printerMsg = "Cambiar impresora a modo impresion";
                return false;
            }

            this.printerMsg = "Error desconocido";
            return lResult == LKPrint.LK_STS_NORMAL;    
            //return true;
        }

        public bool open()
        {
            return LKPrint.OpenPort(this.comPort, DEFAULT_BAUDRATE) == LKPrint.LK_SUCCESS;
            //return true;
        }

        public bool close()
        {
            return LKPrint.ClosePort() == LKPrint.LK_SUCCESS;
            //return true;
        }
    }
}